#include "socket_list.h"
#include "rpc_client.h"
#include "rpcGloableTables.h"
#include "rpc_system_info.h"

int CLIGetSysInfo()
{
	unsigned char request[RPC_REQUEST_LNGTH] = {0};
	unsigned int pkt_len = sizeof(RPC_HEAD_CLIENT_ST);
	unsigned int rpc_id = CCSTAT_SYSINFO_RPC_ID;
	unsigned int param_num = 0;
	int offset = sizeof(RPC_HEAD_CLIENT_ST);
	int i;	
	int fdNum;
	
	/* recv response variables */
	unsigned char response[RPC_REQUEST_LNGTH] = {0};
	int rspLen,ifor,offset2=sizeof(RPC_HEAD_CLIENT_ST);
	int stat = 0,ret;

	/* Build head*/	
	cli_rpc_generate_head(request, pkt_len, rpc_id, param_num);
	/* Take out socket */
	fdNum = socketListOut();

	RPC_HEAD_CLIENT_ST *pHead = (RPC_HEAD_CLIENT_ST *)request;
	
	/* Debug infor */
	printf("pklen = %d\n", pHead->pkt_len);
	printf("param_num = %d\n", pHead->param_num);
	printf("rpc_id = %d\n", pHead->rpc_id);
	printf("fdNum = %d\n", fdNum);
	
	while(fdNum < 0)
	{
		taskDelay(2);
		fdNum = socketListOut();		
	}
	for (i = 0; i < MAX_CC_NUM; i++)
	{
		if (socketList[fdNum].rpc_fd[i] == 0)
			continue;
		/*write request*/

		cliRpcWrite(socketList[fdNum].rpc_fd[i], (char *)request, pkt_len);

		/*recv response*/
		offset2=sizeof(RPC_HEAD_CLIENT_ST);
		memset(response, 0, RPC_REQUEST_LNGTH);

		rspLen = readCCResponse(socketList[fdNum].rpc_fd[i],response);

		printf("cli <CLIGetSysInfo> response rspLen =%d,response = ",rspLen);
		for(ifor=0;ifor<rspLen;ifor++)
			printf("0x%02x ",response[ifor]);
		printf("\r\n");
		cli_rpc_parse_param(response, &offset2, (unsigned char *)&ret);
		printf("CLIGetSysInfo: %d\n", ret);
		stat +=ret;
	}
	socketListIn(fdNum);
	return stat;
}

int CLIGetSysInfo_IP()
{
	unsigned char request[RPC_REQUEST_LNGTH] = {0};
	unsigned int pkt_len = sizeof(RPC_HEAD_CLIENT_ST);
	unsigned int rpc_id = CCSTAT_SYSINFO_RPC_GET_IPADDR;
	unsigned int param_num = 0;
	int offset = sizeof(RPC_HEAD_CLIENT_ST);
	int i;	
	int fdNum;
	
	/* recv response variables */
	unsigned char response[RPC_REQUEST_LNGTH] = {0};
	int rspLen,ifor,offset2=sizeof(RPC_HEAD_CLIENT_ST);
	int stat = 0, ret;

	/* Build head*/	
	cli_rpc_generate_head(request, pkt_len, rpc_id, param_num);
	/* Take out socket */
	fdNum = socketListOut();

	RPC_HEAD_CLIENT_ST *pHead = (RPC_HEAD_CLIENT_ST *)request;
	
	printf("\r\n");
	printf("pklen = %d\n", pHead->pkt_len);
	printf("param_num = %d\n", pHead->param_num);
	printf("rpc_id = %d\n", pHead->rpc_id);
	printf("fdNum = %d\n", fdNum);
	printf("\r\n");
	
	while(fdNum < 0)
	{
		taskDelay(2);
		fdNum = socketListOut();		
	}
	
	for (i = 0; i < MAX_CC_NUM; i++)
	{
		if (socketList[fdNum].rpc_fd[i] == 0)
			continue;
		
		/*write request*/
		cliRpcWrite(socketList[fdNum].rpc_fd[i], (char *)request, pkt_len);
		
		/*recv response*/
		offset2=sizeof(RPC_HEAD_CLIENT_ST);
		memset(response, 0, RPC_REQUEST_LNGTH);
		
		rspLen = readCCResponse(socketList[fdNum].rpc_fd[i],response);
		printf("cli <CLIGetSysInfo> response rspLen =%d,response = ",rspLen);
		
		for(ifor=0;ifor<rspLen;ifor++)
			printf("0x%02x ",response[ifor]);
		printf("\r\n");
		
		cli_rpc_parse_param(response, &offset2, (unsigned char *)&ret);
		
		unsigned char *ptr = (unsigned char *)&ret;
		printf("CLIGetSysInfo_IP: ");
		for(ifor=0;ifor<4;ifor++)
			printf("%d. ",ptr[ifor]);
		printf("\r\n");
		
		stat +=ret;
	}
	socketListIn(fdNum);
	return stat;
}

